package com.bhs.sansonglogistics.ui.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.GlideEngine;
import com.bhs.sansonglogistics.bean.ImageReturn;
import com.bhs.sansonglogistics.bean.Location;
import com.bhs.sansonglogistics.bean.TabBean;
import com.bhs.sansonglogistics.bean.UserDTO;
import com.bhs.sansonglogistics.bean.UserInfoBean;
import com.bhs.sansonglogistics.bean.event.CompanyInfo;
import com.bhs.sansonglogistics.dialog.BottomSelectDialog;
import com.bhs.sansonglogistics.dialog.BottomSelectiveFaceDialog;
import com.bhs.sansonglogistics.dialog.CityPickerDialog;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.dialog.MyClickListener2;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.enterprise.EnterpriseCertificationActivity;
import com.bhs.sansonglogistics.ui.login.RealNameAuthenticationActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.google.gson.Gson;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 个人信息
 */
public class PersonalInformationActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private ShapeableImageView mIvHead;
    private TextView mTvName;
    private TextView mTvSex;
    private TextView mTvRealNameAuthentication;
    private TextView mTvCompanyCertification;
    private TextView mTvCity;
    private UserDTO userDTO;


    @Override
    public int initLayout() {
        return R.layout.activity_personal_information;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("个人信息");
        mIvHead = findViewById(R.id.iv_head);
        findViewById(R.id.ll_name).setOnClickListener(this);
        mTvName = findViewById(R.id.tv_name);
        findViewById(R.id.ll_sex).setOnClickListener(this);
        mTvSex = findViewById(R.id.tv_sex);
        findViewById(R.id.ll_real_name_authentication).setOnClickListener(this);
        mTvRealNameAuthentication = findViewById(R.id.tv_real_name_authentication);
        findViewById(R.id.ll_company_certification).setOnClickListener(this);
        mTvCompanyCertification = findViewById(R.id.tv_company_certification);
        findViewById(R.id.ll_city).setOnClickListener(this);
        mTvCity = findViewById(R.id.tv_city);
        findViewById(R.id.ll_my_circle).setOnClickListener(this);
        mIvHead.setOnClickListener(this);
        findViewById(R.id.tv_change_picture).setOnClickListener(this);


    }

    @Override
    public void initData() {

    }

    BasePopupView basePopupView2;


    @Override
    protected void onResume() {
        super.onResume();
        networkRequest(netApi.get_user_info(), this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_real_name_authentication:
   //             startActivity(new Intent(mActivity, RealNameAuthenticationActivity.class));
                if (mTvRealNameAuthentication.getText().toString().equals("去认证")) {
                    startActivity(new Intent(mActivity, RealNameAuthenticationActivity.class));
                }else {
                    ToastUtil.show("已认证");
                }
                break;
            case R.id.iv_head:
            case R.id.tv_change_picture:
                bottomSelect();
                break;
            case R.id.ll_name:
                startActivity(new Intent(mActivity, EditTextActivity.class)
                        .putExtra("Content", mTvName.getText().toString()));
                break;
            case R.id.ll_sex:
                bottomSelectSex();
                break;
            case R.id.ll_company_certification:
                if (mTvCompanyCertification.getText().toString().equals("去认证")) {
                    startActivity(new Intent(mActivity, EnterpriseCertificationActivity.class)
                            .putExtra("Content", mTvName.getText().toString()).putExtra("type", 1));
                } else {
                    ToastUtil.show("已认证");
                }
                break;
            case R.id.ll_city:
                bottomSelectCity();
                break;
        }
    }


    private void bottomSelectSex() {
        List<TabBean> list = new ArrayList<>();
        list.add(new TabBean("男", "1"));
        list.add(new TabBean("女", "0"));
        if (pvOptions == null) {
            pvOptions = new OptionsPickerBuilder(mActivity, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    mTvSex.setText(list.get(options1).getTypeName());
                    String sex = list.get(options1).getType();
                    networkRequest(netApi.update_user_info("sex", sex), PersonalInformationActivity.this);
                }
            }).build();
            pvOptions.setPicker(list);
        }
        pvOptions.show();

    }

    private OptionsPickerView<TabBean> pvOptions;
    CityPickerDialog cityPickerDialog;

    private void bottomSelectCity() {
        if (cityPickerDialog == null) {
            cityPickerDialog = new CityPickerDialog(this);
            cityPickerDialog.setMyClickListener(new MyClickListener2() {
                @Override
                public void onClick(String id, String info) {
                    String[] address = info.split(",");
                    mTvCity.setText(address[1]);
                    networkRequest(netApi.update_user_info("city", address[1]), PersonalInformationActivity.this);
                }
            });
        }

        cityPickerDialog.show();
        cityPickerDialog.cityPickerReuse.setCity(Location.city);
    }

    private BasePopupView basePopupView;
    private BottomSelectDialog bottomSelectDialog;

    private void bottomSelect() {
        if (basePopupView == null) {
            bottomSelectDialog = new BottomSelectDialog(this);
            bottomSelectDialog.showSelectPicture(1);
            bottomSelectDialog.setMyClickListener(new MyClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getId() == R.id.tv_take_picture) {
                        photograph();
                    } else if (v.getId() == R.id.tv_photo_album) {
                        photoAlbum();
                    } else if (v.getId() == R.id.tv_select_picture) {
                        selectFace();
                    }
                    basePopupView.dismiss();
                }
            });
            basePopupView = new XPopup.Builder(mActivity)
                    .asCustom(bottomSelectDialog)
                    .show();

        } else {
            basePopupView.show();
        }
    }

    BottomSelectiveFaceDialog faceDialog;

    private void selectFace() {
        if (faceDialog == null) {
            faceDialog = new BottomSelectiveFaceDialog(this);
            faceDialog.setMyClickListener(new MyClickListener2() {
                @Override
                public void onClick(String id, String info) {
                    Glide.with(mContext).load(info).into(mIvHead);
                    networkRequest(netApi.update_user_info("headerpic", info), PersonalInformationActivity.this, 9528);
                }
            });
        }
        faceDialog.show();
    }


    private void photograph() {
        PictureSelector.create(this)
                .openCamera(SelectMimeType.ofImage())
                .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        MyUtils.imageUpload(mActivity, result, PersonalInformationActivity.this, 9527);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    private void photoAlbum() {
        PictureSelector.create(this)
                .openGallery(SelectMimeType.ofImage())
                .setMaxSelectNum(1)
                .setImageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        MyUtils.imageUpload(mActivity, result, PersonalInformationActivity.this, 9527);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            UserInfoBean infoBean = new Gson().fromJson(json, UserInfoBean.class);
            if (infoBean.isStatus()) {
                userDTO = infoBean.getData().getUser_info();
                Glide.with(this).load(userDTO.getHeaderpic()).into(mIvHead);
                mTvName.setText(userDTO.getNickname());
                mTvSex.setText(MyUtils.getSexName(userDTO.getSex()));
                mTvRealNameAuthentication.setText(userDTO.getIs_approve() == 1 ? "已认证" : "去认证");
                mTvCity.setText(userDTO.getCity());
                CompanyInfo companyInfo = infoBean.getData().getLogistics_info();
                mTvCompanyCertification.setText(companyInfo.getIs_company_approve() == 1 ? "已认证" : "去认证");
            } else {
                ToastUtil.show(infoBean.getMsg());
            }
        } else if (requestCode == 9527) {
            ImageReturn dataBean = new Gson().fromJson(json, ImageReturn.class);
            if (dataBean.isStatus()) {
                Glide.with(mContext).load(dataBean.getData().getSrc()).into(mIvHead);
                networkRequest(netApi.update_user_info("headerpic", dataBean.getData().getSrc()), PersonalInformationActivity.this, 9528);
            } else {
                loadingPopupDismiss();
                ToastUtil.show(dataBean.getMsg());
            }
        } else if (requestCode == 9528) {
            loadingPopupDismiss();
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }
}